/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.network;

import java.util.Iterator;

import pirates.PiratesServer;
import pirates.Settings;
import pirates.network.messages.*;

import com.jme3.network.Server;
import com.jme3.scene.Spatial;

import pirates.Settings.ShipConstants;
import pirates.notifications.Notify;
import pirates.ship.ShipControl;

/**
 * Used by server
 * 
 * @author samsjo-8
 * 
 */
public class ServerNetSender implements Runnable {

	private Server server;
	private PiratesServer app;
	private boolean running = false;

	public ServerNetSender(Server server, PiratesServer app) {
		super();
		this.server = server;
		this.app = app;
		// start();

	}

	@Override
	public void run() {
		Notify.debug("Started server net send");
		while (true) {
			try {
				Thread.sleep(Settings.SERVER_ENTITY_UPDATE_RATE);
				// Notify.debug("sending ship update");
				Iterator<Spatial> aiShipsIterator = app.ships.getAIships();

				while (aiShipsIterator.hasNext()) {
					Spatial ship1 = aiShipsIterator.next();
					// Notify.debug("sending NW update for "+ship1.getName());
					ShipControl ship = ship1.getControl(ShipControl.class);
					EntityMessage msg = new EntityMessage(
							ship1.getName(),
							ship1.<String>getUserData(ShipConstants.TEAM.toString())
									+ "", // entityTeam
							ship1.<String>getUserData(ShipConstants.ENTITY_MODEL
									.toString()) + "", // entityType
							Float.valueOf(ship1
									.<Float>getUserData(ShipConstants.HITPOINTS
											.toString())
									+ ""), // hitpoints
							ship.getPhysicsLocation(),
							ship.getPhysicsRotation(), ship.getVelocity(),
							ship.getSteer());
					server.broadcast(msg);
				}

			} catch (Exception e) {
				if (Settings.DEBUG) {
					e.printStackTrace();
				}
				Notify.debug("Exception in Client netsend");
			}

		}
	}

	public void close() {
		running = false;
		server.close();

	}
}
